﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssettDatabase
{
    public partial class Form_Assett_Search : Form
    {
        public bool RetireAsset { get; set; } 
        public Form_Assett_Search()
        {
            InitializeComponent();
        }

        private void Btn_Return_Main_Click(object sender, EventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void Form_Assett_Search_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BLL.Asset bllAssett = new BLL.Asset();
            List<VO.Asset> filterData = new List<VO.Asset>();

            filterData = (from a in bllAssett.GetAsset() where a.Serial_Number.ToLower().Contains(TB_SerialNumber.Text.ToLower()) select a).ToList();
            if (CB_Customer.SelectedValue.ToString() != "-1")
                filterData = (from a in filterData where a.Customer_ID.Equals(Convert.ToInt32(CB_Customer.SelectedValue)) select a).ToList();
            if (CB_Asset_Type.SelectedValue.ToString() != "-1")
                filterData = (from a in filterData where a.Asset_Type_ID.Equals(Convert.ToInt32(CB_Asset_Type.SelectedValue)) select a).ToList();

            gridAsset.DataSource = filterData;
        }

        private void LoadAssets()
        {
            gridAsset.Columns.Clear();
            if (RetireAsset)
            { 
                DataGridViewButtonColumn DeleteBtn = new DataGridViewButtonColumn();
                DeleteBtn.UseColumnTextForButtonValue = true;
                DeleteBtn.HeaderText = "Retire Asset";
                DeleteBtn.DataPropertyName = "retireBtn";
                DeleteBtn.Text = "Retire";
                gridAsset.Columns.Add(DeleteBtn);
            }
            BLL.Asset bllAsset = new BLL.Asset();
            gridAsset.DataSource = bllAsset.GetAsset();
        }

        private void Form_Assett_Search_Load(object sender, EventArgs e)
        {
            LoadAssets();
            fill_Customer();
            fill_Asset_Type();
        }

        private void gridAsset_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && gridAsset.Columns[e.ColumnIndex].DataPropertyName == "retireBtn")
            {
                DialogResult dialog = MessageBox.Show("Are you sure you really want to retire this asset?", "Retire asset", MessageBoxButtons.YesNo);
                if (dialog == DialogResult.Yes)
                {
                    try
                    {
                        BLL.Asset bllAsset = new BLL.Asset();
                        bllAsset.Del(Convert.ToInt32(gridAsset.Rows[e.RowIndex].Cells["Asset_ID"].Value.ToString()));
                        LoadAssets();
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        void fill_Asset_Type()
        {
            try
            {
                BLL.Asset_Type bllAsset_Type = new BLL.Asset_Type();
                List<VO.Asset_Type> voAsset_TypeList;
                voAsset_TypeList = bllAsset_Type.GetAsset_Type();
                //add an item for all asset type
                voAsset_TypeList.Add(new VO.Asset_Type { Asset_Type_ID = -1, Asset_Type_Name = "<select one>" });

                CB_Asset_Type.DataSource = voAsset_TypeList;
                CB_Asset_Type.DisplayMember = VO.Asset_Type.Asset_Type_Fields.Asset_Type_Name.ToString();
                CB_Asset_Type.ValueMember = VO.Asset_Type.Asset_Type_Fields.Asset_Type_ID.ToString();
                CB_Asset_Type.SelectedValue = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void fill_Customer()
        {
            try
            {
                BLL.Customer bllCustomer = new BLL.Customer();
                List<VO.Customer> voCustomerList;
                voCustomerList = bllCustomer.GetCustomers();
                //add an item for all customers
                voCustomerList.Add(new VO.Customer { Customer_ID = -1, Customer_Name = "<select one>" });

                CB_Customer.DataSource = voCustomerList;
                CB_Customer.DisplayMember = VO.Customer.Customer_Fields.Customer_Name.ToString();
                CB_Customer.ValueMember = VO.Customer.Customer_Fields.Customer_ID.ToString();
                CB_Customer.SelectedValue = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string columnsHeader = "";

            if (gridAsset.Rows.Count == 0)
                return;

            for (int i = 0; i < gridAsset.Columns.Count; i++)
            {
                columnsHeader += gridAsset.Columns[i].Name + ",";
            }
            sb.Append(columnsHeader + Environment.NewLine);

            foreach (DataGridViewRow gridAssetRow in gridAsset.Rows)
            {
                if (!gridAssetRow.IsNewRow)
                {
                    for (int c = 0; c < gridAssetRow.Cells.Count; c++)
                    {
                        sb.Append(gridAssetRow.Cells[c].Value + ",");
                    }
                    sb.Append(Environment.NewLine);
                }
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV files (*.csv)|*.csv";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName, false))
                {
                    sw.WriteLine(sb.ToString());
                }
            }
            MessageBox.Show("CSV file saved");
        }

        private void gridAsset_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gridAsset.Rows[e.RowIndex].Cells["Asset_ID"] != null)
            {
                FormProvider.MainMenu.Hide();
                Form_Add_Assett f11 = new Form_Add_Assett();
                f11.retAsset_ID = Convert.ToInt32(gridAsset.Rows[e.RowIndex].Cells["Asset_ID"].Value);
                f11.ShowDialog();
            }
        }
    }
}
